﻿using System;
using System.Collections.Generic;
using System.Text;
using Models;
using System.Data.SqlClient;
namespace Service
{
    public class T_CS_COMMUNITY_Service
    {

        static string s_strSql = "";
        public static IList<T_CS_COMMUNITY_Models> s_ArrGetcommunityBySql(string sql, params SqlParameter[] values)
        {
            Cls_DBHelper dBHelper = new Cls_DBHelper();
            List<T_CS_COMMUNITY_Models> list = new List<T_CS_COMMUNITY_Models>();
            SqlDataReader reader = null;
            try
            {


                if (values == null)
                {
                    reader = dBHelper.m_clsGetReader(sql);
                }
                else
                {
                    reader = dBHelper.m_clsGetReader(sql, values);
                }
                while (reader.Read())
                {
                    T_CS_COMMUNITY_Models communt = new T_CS_COMMUNITY_Models();
                    communt.LoadModels(reader);
                    list.Add(communt);
                }

            }
            //catch ()
            //{
               
            //}
            finally
            {
                if (reader != null && !reader.IsClosed)
                {
                    reader.Close();
                }
            }
            return list;
        }
        //查询所有
        public static IList<T_CS_COMMUNITY_Models> Get_T_CS_COMMUNITY_All()
        {
            s_strSql = "select * from T_CS_COMMUNITY";

            return s_ArrGetcommunityBySql(s_strSql);
        }
        //查询子社区
        public static IList<T_CS_COMMUNITY_Models> Get_T_CS_COMMUNITY_ByCode(string code)
        {
            s_strSql = "select * from T_CS_COMMUNITY where PARENTCOMMUNITY_CHR=@PARENTCOMMUNITY_CHR";
            SqlParameter para = new SqlParameter("@PARENTCOMMUNITY_CHR", code);
            return s_ArrGetcommunityBySql(s_strSql, para);
        }
        //根据ID查找
        public static T_CS_COMMUNITY_Models Get_T_CS_COMMUNITY_id(int id)
        {
            T_CS_COMMUNITY_Models community = new T_CS_COMMUNITY_Models();
            s_strSql = "select * from T_CS_COMMUNITY where SEQID_INT=@SEQID_INT";
            SqlParameter para = new SqlParameter("@SEQID_INT", id);
            IList<T_CS_COMMUNITY_Models> list = s_ArrGetcommunityBySql(s_strSql, para);
            if (list.Count != 0)
                community = list[0];
            return community;
        }
        //删除根据ID
        public static int Delete_T_T_CS_COMMUNITY_ById(int id)
        {
            s_strSql = "delete from T_CS_COMMUNITY where SEQID_INT=@SEQID_INT";
            SqlParameter para = new SqlParameter("@SEQID_INT", id);
            Cls_DBHelper dbhelper = new Cls_DBHelper();
            return dbhelper.m_intExecuteCommand(s_strSql, para);
        }
        //根据ID 修改
        public static int Modify_T_CS_COMMUNITY_ById(T_CS_COMMUNITY_Models community)
        {
            s_strSql = "UPDATE T_CS_COMMUNITY SET COMMUNITYCODE_CHR = @COMMUNITYCODE_CHR,COMMUNITYNAME_VCHR = @COMMUNITYNAME_VCHR,PARENTCOMMUNITY_CHR =@PARENTCOMMUNITY_CHR,POSTALCODE_CHR =@POSTALCODE_CHR ,COMMUNITYLEVEL_INT = @COMMUNITYLEVEL_INT,REGIONALISMCODE_VCHR =@REGIONALISMCODE_VCHR,CURRENTCS_INT = @CURRENTCS_INT WHERE SEQID_INT = @SEQID_INT";
            SqlParameter[] para_Arr = new SqlParameter[]{                         
            new SqlParameter("@COMMUNITYCODE_CHR",community.COMMUNITYCODE_CHR),
            new SqlParameter("@COMMUNITYNAME_VCHR",community.COMMUNITYNAME_VCHR),
            new SqlParameter("@PARENTCOMMUNITY_CHR",community.PARENTCOMMUNITY_CHR),
            new SqlParameter("@POSTALCODE_CHR",community.POSTALCODE_CHR),
            new SqlParameter("@COMMUNITYLEVEL_INT",community.COMMUNITYLEVEL_INT),
            new SqlParameter("@REGIONALISMCODE_VCHR",community.REGIONALISMCODE_VCHR),
            new SqlParameter("@CURRENTCS_INT",community.CURRENTCS_INT),
            new SqlParameter("@SEQID_INT",community.SEQID_INT)
            };
            Cls_DBHelper dbhelper = new Cls_DBHelper();
            return dbhelper.m_intExecuteCommand(s_strSql, para_Arr);
        }
    }
}
